package com.david.rabbitmq.direct;

import com.david.rabbitqm.util.ChannelUtlis;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;

/**
 * @author David
 * @version V1.0
 * @Package com.david.rabbitmq.direct
 * @date 2021/7/17 10:09
 */
public class ErrorLogConsumer {

    public static void main(String[] args) throws IOException {
        Channel channel = ChannelUtlis.getChannel();
        channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        String queueName = channel.queueDeclare().getQueue();
        System.out.println("Error Log Queue :" + queueName);
        // 将logs交换器和queueName队列通过bindingKey：warn
        channel.queueBind(queueName, "direct_logs", "error");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(queueName, deliverCallback, consumerTag -> {});
    }
}
